/**
 * 类名：PreviewAction
 * 创建日期： 2014-11-21 下午7:39:57
 * 
 */
package com.erms.action;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.erms.bean.FileInfo;
import com.erms.domain.CoursewareService;
import com.erms.domain.FileService;
import com.erms.util.ImageUtil;

/**
 * @author hmb
 * 备注： 
 *
 */
public class PreviewAction extends BaseAction {
	private int id;
	private InputStream inputStream;
	@Resource
	private CoursewareService coursewareService;
	@Resource
	private FileService fileService;
	
	private String text = "这是服务器的文本";
	private static final long	serialVersionUID	= 2850445270789334326L;
	public String getText() {

		return text;
	}
	public void setText(String text) {

		this.text = text;
	}
	/**
	 * @see com.erms.action.BaseAction#execute()
	 * @return
	 * @throws Exception
	 */
	@Override
	public String execute() throws Exception {
		//最大宽高度500
		logger.debug("开始读取图片");
		/*FileInfo fileInfo = fileService.find(id);
		if (fileInfo == null) {
			logger.debug("不存在该文件,id="+id);
			return null;
		}
		Courseware courseware = coursewareService.find(id);
		if (courseware == null) {
			logger.debug("不存在该课件,id="+id);
			return null;
		}
		File file = new File("d:/test/184.jpg");
		BufferedImage image = ImageUtil.getImage(file, 500);
		inputStream = ImageUtil.toInputStream(image);
		*/
		FileInfo fileInfo = fileService.find(id);
		if(fileInfo == null) return null;
		switch (fileInfo.getType()) {
			case "image/jpeg":
			case "image/png":
			case "image/gif":				
				break;
			case "text/xml":
				break;
			case "application/octet-stream":
				//break;
			case "text/plain":
				JSONObject object = new JSONObject();
				object.put("data", fileService.previewTEXT(id));
				text = object.toString();
				//getRequest().setAttribute("text", "这是服务器上的文本");
				logger.debug("请求预览的目标是文本文件");
				return "text";
			case "application/pdf":
				break;
			case "application/msword":
				//break;
			case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
				JSONObject word = new JSONObject();
				word.put("data", fileService.previewWord(id));
				text = word.toString();
				//getRequest().setAttribute("text", "这是服务器上的文本");
				logger.debug("请求预览的目标是word文件");
				return "text";
				//break;
			case "application/vnd.ms-powerpoint":
				List<Map<String, String>> pptList = fileService.previewPPT(id);
				if (pptList == null) {
					return null;
				}
				for (Map<String, String> map : pptList) {
					logger.debug(map.get("title"));
					logger.debug(map.get("des"));
					logger.debug(map.get("img"));
				}
				JSONArray ppt =JSONArray.fromObject(pptList);
				text = ppt.toString();
				logger.debug("请求预览的目标是ppt文件");
				return "text";
			case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
				break;
			case "application/vnd.ms-excel":
				break;
			case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
				break;
			case "application/zip":
				break;
			
			default:
				break;
		
		}
		inputStream = fileService.download(id);
		return SUCCESS;
	}
	public InputStream getInputStream() {

		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {

		this.inputStream = inputStream;
	}
	public int getId() {

		return id;
	}
	public void setId(Integer id) {

		this.id = id;
	}

}
